<template>
    <div class="madp-customer-detail" v-if="!loading">
        <div class="container">
            <div v-if="chatId" style="margin-bottom: 10px">
                <div class="switchMemBtn cursor-pointer">
                    <i class="el-icon-sort" @click="memberListVisible = true"></i>
                </div>
            </div>
            <div v-if="wxExternalUserid">
                <div class="header">
                    <img class="activate" :src="basicInfo.avatar || $defaultAvatar" alt="" />
                    <div class="name">
                        <span class="text-one-ellipsis">{{ basicInfo.remark }}</span>
                        <span class="text-one-ellipsis">@{{ basicInfo.typeText }}</span>
                    </div>
                    <div class="icon-tools">
                        <img class="cursor-pointer" src="@/assets/img/customerDetail/tianjiagenjin.png" alt="" @click="openDialog(2)" />
                        <img class="cursor-pointer" src="@/assets/img/customerDetail/tianjiadaiban.png" alt="" @click="openDialog(1)" />
                    </div>
                </div>
                <div class="menu">
                    <div :class="{ item: true, 'cursor-pointer': true, active: menuActive == 1 }" @click="selectMenu(1)">
                        <span>基本信息</span>
                        <span v-show="menuActive == 1"></span>
                    </div>
                    <div :class="{ item: true, 'cursor-pointer': true, active: menuActive == 2 }" @click="selectMenu(2)">
                        <span>画像信息</span>
                        <span v-show="menuActive == 2"></span>
                    </div>
                    <div :class="{ item: true, 'cursor-pointer': true, active: menuActive == 3 }" @click="selectMenu(3)">
                        <span>互动轨迹</span>
                        <span v-show="menuActive == 3"></span>
                    </div>
                </div>
                <div class="main">
                    <basic-info v-if="menuActive == 1" :info="basicInfo"></basic-info>
                    <picture-info v-else-if="menuActive == 2" :wxUserId="wxUserId" :wxExternalUserid="wxExternalUserid" :assetInfo="basicInfo.customerAssetsInfoVO"></picture-info>
                    <dynamic-info v-else-if="menuActive == 3" :wxUserId="wxUserId" :wxExternalUserid="wxExternalUserid" :isUpdate.sync="isUpdate"></dynamic-info>
                </div>
            </div>
            <member-list-component :wxUserId="wxUserId" :chatId="chatId" :visible.sync="memberListVisible" @memberInfoFn="memberInfoFn"></member-list-component>
            <add-info-component :dialogTitle="dialogTitle" :visible.sync="dialogVisible" @confirm="confirmDialog" :dialogType="dialogType"></add-info-component>
        </div>
    </div>
</template>
<script>
    import basicInfo from '@/components/customer-detail/basic-info';
    import pictureInfo from '@/components/customer-detail/picture-info';
    import dynamicInfo from '@/components/customer-detail/dynamic-info';
    import AddInfoComponent from '@/components/customer-detail/add-info.vue';
    import MemberListComponent from '@/components/choose-member-dialog.vue';
    import * as customerDetailApi from '@/api/customer-details';

    import mxInitWxConfig from '@/mixin/mxInitWxConfig.js';
    export default {
        mixins: [mxInitWxConfig],
        components: {
            basicInfo,
            pictureInfo,
            dynamicInfo,
            AddInfoComponent,
            MemberListComponent
        },
        data() {
            return {
                memberListVisible: false,
                loading: true,
                isUpdate: 0, // 1-更新待办列表 2-更新跟进列表
                inputVal: '',
                dateTime: '',
                menuActive: 1,
                basicInfo: {},
                dialogVisible: false,
                dialogType: 1,
                dialogTitle: ''
            };
        },
        watch: {
            wxExternalUserid(nval, oval) {
                if (this.wxExternalUserid && this.wxUserId) {
                    this.getBasicInfo();
                }
            },
            wxUserId(nval, oval) {
                if (this.wxExternalUserid && this.wxUserId) {
                    this.getBasicInfo();
                }
                if (this.wxUserId && this.chatId) {
                    this.loading = false;
                    this.memberListVisible = true;
                }
                if (this.wxUserId && this.entry == 'normal' && this.isSharePage != 1) {
                    this.loading = false;
                    this.memberListVisible = true;
                }
            },
            chatId() {
                if (this.wxUserId && this.chatId) {
                    this.loading = false;
                    this.memberListVisible = true;
                }
            },
            entry() {
                if (this.wxUserId && this.entry == 'normal' && this.isSharePage != 1) {
                    this.loading = false;
                    this.memberListVisible = true;
                }
            }
        },
        created() {},
        activated() {
            if (this.$route.query.wxUserId) {
                this.wxUserId = this.$route.query.wxUserId || '';
            }
            if (this.$route.query.wxExternalUserid) {
                this.wxExternalUserid = this.$route.query.wxExternalUserid || '';
            }
        },
        methods: {
            memberInfoFn(info) {
                this.memberListVisible = false;
                this.wxExternalUserid = info.wxExternalUserid;
            },
            openDialog(type) {
                this.dialogType = type;
                if (type == 1) {
                    this.dialogTitle = '添加待办';
                } else if (type == 2) {
                    this.dialogTitle = '添加跟进';
                }
                this.dialogVisible = true;
            },
            confirmDialog(info) {
                if (this.dialogType == 1) {
                    customerDetailApi
                        .addTodo({
                            doBody: info.inputVal,
                            startTime: info.dateTime,
                            wxUserId: this.wxUserId,
                            wxExternalUserid: this.wxExternalUserid
                        })
                        .then((res) => {
                            this.inputVal = '';
                            this.$toast({ text: '新增待办成功' });
                            this.dialogVisible = false;
                            this.isUpdate = 1;
                        });
                } else if (this.dialogType == 2) {
                    customerDetailApi
                        .addFollow({
                            followBody: info.inputVal,
                            wxUserId: this.wxUserId,
                            wxExternalUserid: this.wxExternalUserid
                        })
                        .then((res) => {
                            this.inputVal = '';
                            this.$toast({ text: '新增跟进成功' });
                            this.dialogVisible = false;
                            this.isUpdate = 2;
                        });
                }
            },
            selectMenu(type) {
                this.menuActive = type;
            },
            // 获取客户基本信息
            getBasicInfo() {
                customerDetailApi
                    .getBasicInfo({
                        wxUserId: this.wxUserId, //当前用户userid(成员userid)
                        wxExternalUserid: this.wxExternalUserid //当前联系人userid
                    })
                    .then((res) => {
                        let info = res.data.data;
                        if (!info.customerAssetsInfoVO) info.customerAssetsInfoVO = {};
                        this.basicInfo = info;
                        this.loading = false;
                    });
            }
        }
    };
</script>

<style lang="scss" scoped>
    .madp-customer-detail {
        width: 100%;
        background-image: url('../assets/img/customerDetail/beijing.png');
        background-repeat: no-repeat;
        background-size: 100%;
        background-color: #fafafa;
        height: 100vh;
        overflow-y: scroll;
        .container {
            width: 100%;
            padding: 16px;
            .switchMemBtn {
                font-size: 20px;
                transform: rotate(90deg);
                background-color: rgba(251, 83, 83, 0.67);
                border-radius: 50%;
                width: 30px;
                height: 30px;
                display: flex;
                justify-content: center;
                align-items: center;
                color: #fff;
            }
            .header {
                display: flex;
                justify-content: space-around;
                align-content: center;
                .activate {
                    width: 40px;
                    height: 40px;
                    border-radius: 50%;
                }
                .name {
                    flex: 1;
                    display: flex;
                    margin-left: 8px;
                    flex-direction: column;
                    span {
                        width: 180px;
                    }
                    :nth-child(1) {
                        font-size: 20px;
                        font-family: PingFangSC-Medium;
                        font-weight: 500;
                        color: #2c2c2c;
                    }
                    :nth-child(2) {
                        font-size: 14px;
                        font-family: PingFang SC;
                        font-weight: 500;
                        color: #58be6b;
                    }
                }
                .icon-tools {
                    display: flex;
                    img {
                        width: 40px;
                        height: 40px;
                        margin-left: 8px;
                    }
                }
            }
            .menu {
                margin-top: 16px;
                padding: 0 16px;
                padding-top: 12px;
                padding-bottom: 5px;
                display: flex;
                justify-content: space-between;
                background: #ffffff;
                box-shadow: 0px 3px 6px 0px rgba(181, 181, 181, 0.16);
                border-radius: 8px;
                .item {
                    font-size: 14px;
                    font-family: PingFangSC-Regular, PingFang SC;
                    font-weight: 400;
                    color: rgba(44, 44, 44, 0.4);
                    line-height: 20px;
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    :nth-child(2) {
                        margin-top: 5px;
                        width: 16px;
                        height: 4px;
                        background: #ed5555;
                        border-radius: 2px;
                    }
                }
                .active {
                    color: #2c2c2c;
                }
            }
            .main {
                margin-top: 10px;
            }
        }
    }
</style>
